package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.TipoModelo;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOTipoModelo;

public class MySQLDAOTipoModelo implements DAOTipoModelo {
	
	private TipoModelo tipomodelo = null;
	
	
	public MySQLDAOTipoModelo() {
	}

	public int insert(TipoModelo tipomodelo) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into TipoModelo ("
					+ "Id_TipoModelo, Nombre "
					+ ") " + "values (?, ?) ");

			stmt.setLong(1, tipomodelo.getNroTipoModelo());
			stmt.setString(2, tipomodelo.getNombre());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(TipoModelo tipomodelo) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update TipoModelo " 
							+ "set Nombre = ?"
							+ " where id_TipoModelo = ? ");

			stmt.setString(1, tipomodelo.getNombre());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(TipoModelo tipomodelo) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from TipoModelo where id_TipoModelo = ? ");
			stmt.setLong(1, tipomodelo.getNroTipoModelo());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<TipoModelo> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<TipoModelo> tipomodelos = new ArrayList<TipoModelo>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from TipoModelo");
			rs = stmt.executeQuery();

			while (rs.next()) {
				TipoModelo tipomodelo = new TipoModelo();
				tipomodelo.setNroTipoModelo(rs.getInt("Id_TipoModelo"));
				tipomodelo.setNombre(rs.getString("Nombre"));
				tipomodelos.add(tipomodelo);
			}

			return tipomodelos;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public TipoModelo findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		tipomodelo = new TipoModelo();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from TipoModelo where Id_TipoModelo = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   tipomodelo.setNroTipoModelo(rs.getInt("Id_TipoModelo"));
            tipomodelo.setNombre(rs.getString("Nombre"));
			}

			return tipomodelo;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(TipoModelo tipomodelo) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from TipoModelo where UCASE(nombre)");

			stmt.setString(1, tipomodelo.getNombre().toUpperCase());
//			stmt.setString(2, tipomodelo.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
